package com.dongecloud.hystrix;

import com.donge.bean.User;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
public class UserController {
    @Autowired
    UserService userService;
    @GetMapping("/user")
    public void getUserById() throws ExecutionException, InterruptedException {
        HystrixRequestContext ctx = HystrixRequestContext.initializeContext();

        UserCollapseCommand cmd1 = new UserCollapseCommand(99,userService);
        UserCollapseCommand cmd2 = new UserCollapseCommand(98,userService);
        UserCollapseCommand cmd3 = new UserCollapseCommand(97,userService);
        UserCollapseCommand cmd4 = new UserCollapseCommand(96,userService);

        Future<User> q1 = cmd1.queue();
        Future<User> q2 = cmd2.queue();
        Future<User> q3 = cmd3.queue();
        Future<User> q4 = cmd4.queue();

        User user = q1.get();
        User user1 = q2.get();
        User user2 = q3.get();
        User user3 = q4.get();

        System.out.println(user);
        System.out.println(user1);
        System.out.println(user2);
        System.out.println(user3);

        ctx.close();
    }

    @GetMapping("/user2")
    public void getUserById2() throws ExecutionException, InterruptedException {
        HystrixRequestContext ctx = HystrixRequestContext.initializeContext();

        Future<User> q1 = userService.getUserById(99);
        Future<User> q2 = userService.getUserById(98);
        Future<User> q3 = userService.getUserById(97);
        Future<User> q4 = userService.getUserById(96);

        User user = q1.get();
        User user1 = q2.get();
        User user2 = q3.get();
        User user3 = q4.get();

        System.out.println(user);
        System.out.println(user1);
        System.out.println(user2);
        System.out.println(user3);

        ctx.close();
    }
}
